﻿using System;
using XCI.Core;
using XCI.Helper;

namespace XCI.Extensions
{
    public static class BoolResultExtensions
    {
        /// <summary>
        /// 成功回调函数
        /// </summary>
        public static BoolResult IfSuccess(this BoolResult boolResult, Action<BoolResult> action)
        {
            action?.Invoke(boolResult);
            return boolResult;
        }

        /// <summary>
        /// 失败回调函数
        /// </summary>
        public static BoolResult IfFail(this BoolResult boolResult, Action<BoolResult> action)
        {
            action?.Invoke(boolResult);
            return boolResult;
        }

        ///// <summary>
        ///// 确保Api消息成功,否则抛出异常
        ///// </summary>
        //public static BoolResult EnsureSuccess(this BoolResult boolResult)
        //{
        //    if (!boolResult.Success)
        //    {
        //        throw new WebApiException(string.IsNullOrEmpty(boolResult.Msg) ? "远程服务发生错误" : boolResult.Msg);
        //    }
        //    return boolResult;
        //}

        ///// <summary>
        ///// 确保Api消息成功,否则抛出异常
        ///// </summary>
        //public static BoolResult EnsureApiResultSuccess(this BoolResult boolResult)
        //{
        //    if (!boolResult.Success)
        //    {
        //        throw new WebApiException(string.IsNullOrEmpty(boolResult.Msg) ? "远程服务发生错误" : boolResult.Msg);
        //    }
        //    return boolResult;
        //}

        /// <summary>
        /// 显示操作结果
        /// </summary>
        /// <param name="result">操作结果</param>
        public static bool ShowValidatorMessageBox(this BoolResult result)
        {
            if (!string.IsNullOrEmpty(result.Msg))
            {
                MessageBoxHelper.ShowWarn(result.Msg);
            }
            return result.Success;
        }

        /// <summary>
        /// 显示操作结果
        /// </summary>
        /// <param name="result">操作结果</param>
        public static bool ShowResultMessageBox(this BoolResult result)
        {
            if (!string.IsNullOrEmpty(result.Msg))
            {
                if (result.Success)
                {
                    MessageBoxHelper.ShowMessage(result.Msg);
                }
                else
                {
                    MessageBoxHelper.ShowError(result.Msg);
                }
            }

            return result.Success;
        }
    }

    public static class BoolMessageTExtensions
    {
        /// <summary>
        /// 成功回调函数
        /// </summary>
        public static BoolResult<T> IfSuccess<T>(this BoolResult<T> boolResult, Action<BoolResult<T>> action)
        {
            action?.Invoke(boolResult);
            return boolResult;
        }

        /// <summary>
        /// 失败回调函数
        /// </summary>
        public static BoolResult<T> IfFail<T>(this BoolResult<T> boolResult, Action<BoolResult<T>> action)
        {
            action?.Invoke(boolResult);
            return boolResult;
        }

        ///// <summary>
        ///// 确保Api消息成功,否则抛出异常
        ///// </summary>
        //public static BoolResult<T> EnsureApiResultSuccess<T>(this BoolResult<T> boolResult)
        //{
        //    if (!boolResult.Success)
        //    {
        //        throw new WebApiException(string.IsNullOrEmpty(boolResult.Msg) ? "执行远程服务器程序发生错误,请联系软件开发商" : boolResult.Msg);
        //    }
        //    return boolResult;
        //}

        /// <summary>
        /// 转为消息对象
        /// </summary>
        public static BoolResult AsMessage<T>(this BoolResult<T> boolResult)
        {
            return new BoolResult(boolResult.Success, boolResult.Msg, boolResult.Data);
        }
    }
}